package com.mall.common.utils;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson2.JSONObject;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author: zhaoyongke
 * @create: 2024-04-18 16:52
 * @description: 5656
 */
public class MiniProgramCodeUtil {

    public static String  getMiniProgramCode( String  accessToken) throws Exception {
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));

        // 构建请求JSON数据
        JSONObject requestData = new JSONObject();
        requestData.put("scene", "yuserid="+SecurityUtils.getUserIdStr());
        requestData.put("page", "pages/login/login");
        requestData.put("check_path", false);
        requestData.put("env_version", "release");
        writer.write(requestData.toString());
        writer.flush();
        writer.close();

        InputStream inputStream = connection.getInputStream();
        String encode = Base64.encode(inputStream);
        // inputStream 转成base64

        // 本地文件上传到阿里云
//        String ossUrl = OssFileuploadUtil.getOssUrl(inputStream, IdUtils.createYqm() + ".jpg");
        inputStream.close();
        return encode;
    }


//    public static void main(String[] args) {
//        try {
//            String scene = "id=1778957560112738306";
//            String page = "pages/index/index";
//            String filePath = "D:/cc/rcode222.jpg";
//            MiniProgramCodeUtil.getMiniProgramCode("79_hrxVkC_2mPRSZ7PIN0792Va755JymrG7505tpZnbq8GCX7UCUg4AqebbujhmEXWHnqo0fs81kzAKSbfpPYXXYHqe3uPG86axbGJE3psphWMUp7Xvi7wu8Z1Hi04OOYhAIAWWG",filePath);
//            System.out.println("小程序码已保存至：" + filePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }




}
